// pages/live2/live2.js
import { throttle } from '../../../utils/util.js';
import { cameraControl,resumeLive,dropLive,describeLiveStreamState,forbidLive } from '../../../public/api/device.js'
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accessToken: "",
    deviceSerial: "",
    cameraStream: "",
    deviceName: '',
    translations: {},
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let id = options ? options.id : this.data.deviceSerial
    this.setData({ 
      cameraStream: `https://play.wxidev.top/live/${id}.m3u8`,
      deviceSerial: id
    })
    this.resumeLive(id)
  },

  // 禁推直播流
  forbidLive(streamName) {
    let data = {}
    data.streamName = streamName ? streamName : this.data.deviceSerial
    data.domainName = 'plug.wxidev.top'
    data.appName = 'live'
    data.resumeTime = 7776000
    forbidLive(data).then(res => {
      if(res.data.code === 1) {

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  // 恢复直播推流
  resumeLive(streamName) {
    let data = {}
    data.streamName = streamName ? streamName : this.data.deviceSerial
    data.domainName = 'plug.wxidev.top'
    data.appName = 'live'
    resumeLive(data).then(res => {
      if(res.data.code === 1) {
        this.describeLiveStreamState(streamName)
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  // 断流
  dropLive(streamName) {
    let data = {}
    data.streamName = streamName ? streamName : this.data.deviceSerial
    data.domainName = 'plug.wxidev.top'
    data.appName = 'live'
    dropLive(data).then(res => {
      if(res.data.code === 1) {

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  // 查询流状态
  describeLiveStreamState(streamName) {
    let data = {}
    data.streamName = streamName ? streamName : this.data.deviceSerial
    data.domainName = 'plug.wxidev.top'
    data.appName = 'live'
    describeLiveStreamState(data).then(res => {
      if(res.data.code === 1) {

      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  doHandle: throttle(function(e) {
    let style = e.currentTarget.dataset.id
    let that = this
    if(style == 'on' || style == 'off') {
      that.doCameraControl(that.data.deviceSerial, style)
    } else if(style == 'refresh') {
      that.setData({
        cameraStream: "" // 清空 src，先让 video 停止播放
      });
      setTimeout(() => {
        that.setData({
          cameraStream: `https://play.wxidev.top/live/${that.data.deviceSerial}.m3u8`
        })
      }, 500); // 延时以确保重新加载
    }
  }, 1500),

  // 修改摄像头状态
  doCameraControl(id, style) {
    // if(style == 'on') {
    //   this.resumeLive()
    // } else if(style == 'off') {
    //   this.forbidLive()
    // }
    // return
    let data = {}
    data.uuid = id ? id : this.data.deviceSerial
    data.style = style
    cameraControl(data).then(res => {
      if(res.data.code === 1) {
        this.onLoad()
      } else {
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
      }
    })
  },

  // 返回上一页
  pageBack() {
    setTimeout(() => {
      wx.navigateBack({
        delta: 1
      })
    }, 1500)
  },

  binderror() {
    console.log('出错了');
    let that = this
    return
    wx.showToast({
      title: '视频播放出错了',
      icon: 'none',
      duration: 1000,
      success() {
        setTimeout(() => {
          that.pageBack()
        }, 1000)
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    const translations = app.globalData.translations;
    this.setData({
      translations
    });
    wx.setNavigationBarTitle({
      title: translations.nav.camera
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    // this.doCameraControl(this.data.deviceSerial, 'off')
    // this.dropLive(this.data.deviceSerial)
    this.forbidLive(this.data.deviceSerial)
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})